- page_title = @backlog.is_snapshot? ? (@backlog.snapshot_master.present? ? 'Snapshot' : 'Sprint snapshot') + ' settings' : 'Backlog settings'
- title page_title

- content_for :head do
  = javascript_include_tag 'backlog'
  = stylesheet_link_tag 'backlog'
  = javascript_include_tag 'validation'
  = javascript_include_tag 'backlog_settings'

- content_for :title do
  .heading
    %h1= page_title
  - unless @backlog.is_snapshot?
    #backlog-settings-tabs
      %ul.infinite-tabs

- content_for :global_nav_left do
  .logo
    = link_to root_path do
      .icon
    - if @backlog.snapshot_master.present?
      = link_to raw('← Back to snapshot'), snapshot_account_backlog_path(@backlog.account, @backlog.snapshot_master, @backlog), :id => 'back-to-backlog'
    - elsif @backlog.snapshot_for_sprint.present?
      = link_to raw('← Back to sprint snapshot'), snapshot_account_backlog_path(@backlog.account, @backlog.backlog_root, @backlog), :id => 'back-to-backlog'
    - else
      = link_to raw('← Back to backlog'), account_backlog_path(@backlog.account, @backlog), :id => 'back-to-backlog'

- content_for :side_panel do
  - if is_account_admin? && !@backlog.is_snapshot?
    .backlog-tool-panel
      %h2 Manage users
      Manage the users who have access to this backlog
      .button-container
        = link_to 'Manage backlog users', account_backlog_users_path(current_account, @backlog), :class => 'button'

  - unless @backlog.is_snapshot? || cannot?(:full)
    .backlog-tool-panel
      %h2 Duplicate backlog
      Create a duplicate copy of this backlog including all stories and acceptance criteria.  Sprints will not be copied however.
      .button-container
        = link_to 'Duplicate backlog', duplicate_account_backlog_path(@backlog.account, @backlog), :class => 'button'

  - if @backlog.snapshot_master.present? && can?(:full)
    .backlog-tool-panel
      %h2 Delete snapshot
      Deleting a snapshot immediately and
      %b permanently deletes
      this snapshot.
      %b There is no undo
      so make sure you're absolutely sure you want to delete this snapshot.
      .button-container
        = link_to 'Yes, I understand — delete this snapshot', snapshot_account_backlog_path(@backlog.account, @backlog.snapshot_master, @backlog), :method => 'delete', :title => 'Delete snapshot', :confirm => 'This snapshot will be permanently deleted and cannot be recovered. Are you sure?', :class => 'delete-backlog'
  - elsif @backlog.snapshot_for_sprint.present? && can?(:full)
    .backlog-tool-panel
      %h2 Delete sprint snapshot?
      You cannot delete a sprint snapshot as snapshots are needed to calculate your burn up progress.
  - elsif cannot?(:full)
    .backlog-tool-panel
      %h2 Backlog read only
      You do not have permission to edit any of the settings of this backlog.
  - else
    .backlog-tool-panel
      %h2 Delete backlog
      Deleting a backlog immediately and
      %b permanently deletes
      all the data associated with this backlog.
      %b There is no undo
      so make sure you're absolutely sure you want to delete this backlog.
      .button-container
        = link_to 'Yes, I understand — delete this backlog', account_backlog_path(@backlog.account, @backlog), :method => 'delete', :title => 'Delete backlog', :confirm => 'This backlog will be permanently deleted and cannot be recovered. Are you sure?', :class => 'delete-backlog'

.backlog-settings-body
  = form_for(@backlog, :url => account_backlog_path(current_account, @backlog)) do |f|
    - unless @backlog.errors.blank?
      .form_errors
        We were unable to update the backlog settings.  Please correct the following fields first:
        %ul
          - @backlog.errors.full_messages.each do |error|
            %li
              = error

    != render :partial => 'form', :locals => { :f => f }

    - unless @backlog.is_snapshot?
      .label-spacer
      = f.label :status, "Backlog Status: This backlog is..."
      .form-box
        .input-row
          = f.radio_button :archived?, false
          = label_tag 'backlog_archived_false', raw('Active <span class="detail">— Fully functional backlog</span>')
        .input-row
          = f.radio_button :archived?, true
          = label_tag 'backlog_archived_true', raw('Archived <span class="detail">— This backlog is locked: it can be viewed but not edited</span>')

    %br/
    - if @backlog.archived? && can?(:full)
      = f.submit "Update backlog archive status"
      = link_to "← Back", account_backlog_path(@backlog.account, @backlog), :'data-message'=>'discard changes'
    - elsif @backlog.snapshot_master.present?
      = link_to "← Back to snapshot", snapshot_account_backlog_path(@backlog.account, @backlog.snapshot_master, @backlog), :class => 'button'
    - elsif @backlog.snapshot_for_sprint.present?
      = link_to "← Back to sprint snapshot", snapshot_account_backlog_path(@backlog.account, @backlog.backlog_root, @backlog), :class => 'button'
    - elsif cannot?(:full)
      = link_to "← Back to backlog", account_backlog_path(@backlog.account, @backlog), :class => 'button'
    - else
      = f.submit "Update backlog settings"
      = link_to "← Back", account_backlog_path(@backlog.account, @backlog), :id => 'cancel_backlog_update', :'data-message'=>'discard changes'

- if @backlog.is_snapshot?
  :javascript
    $(function() {
      App.Views.BacklogCreateUpdateMethods.initializeManageBacklog();
    })
- else
  :javascript
    $(function() {
      if ($.datepicker.regional['#{current_locale_code}']) {
        $.datepicker.setDefaults($.datepicker.regional['#{current_locale_code}']);
      }

      var router = new App.Routers.BacklogSettings();

      var backlog = new Backlog({ id: #{@backlog.id}, velocity: #{@backlog.velocity.blank? ? 'null' : @backlog.velocity}, is_editable: #{@backlog.editable? && can?(:full)} });
      App.Collections.SprintStoryStatuses = new SprintStoryStatusesCollection(#{embedded_json_safe sprint_story_statuses_json});
      App.Collections.Sprints = new SprintsCollection(#{embedded_json_safe sprints_json(@backlog)}, { backlog: backlog });

      var sprintTabs = new App.Views.SprintTabs.Index({ collection: App.Collections.Sprints, el: $('#backlog-settings-tabs'), router: router });
      sprintTabs.render();

      router.setTabsReference(sprintTabs);
      Backbone.history.start();
    });